<?php

namespace frontend\controllers;
use frontend\models\ArtForm;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\base\ErrorException;
use yii\helpers\Html;
use common\widgets\file_upload\FileUpload;
use common\models\member\Member;

class ArtController extends Controller
{     
	public $layout = 'ucenter';	
	public function actions()
    {
        return [
            'upload'=>[
                'class' => 'common\widgets\file_upload\UploadAction',     //这里扩展地址别写错
                'config' => [
                ]
            ],
			'ueditor'=>[
				'class' => 'common\widgets\ueditor\UeditorAction',
				'config'=>[
					//上传图片配置
					'imageUrlPrefix' => "", /* 图片访问路径前缀 */
					'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
				]
			]
        ];
    } 
     //添加文章
	  public function actionIndex(){      
        $info = ArtForm::find()->where('id')->asArray()->one();
		$title = $info['title'];
		$intro = $info['intro'];
		$img = $info['img'];
		$link = $info['link'];
		$content = $info['content'];
		$seokeyword = $info['seokeyword'];
		$seocontent = $info['seocontent'];
        return $this->render('index',[
            'title' => $title,          
            'intro' => $intro,
            'img' => $img,
            'link' => $link,
			'content' => $content,
            'seokeyword' => $seokeyword,
            'seocontent' => $seocontent,

        ]);
    }
	public function actionInsertindex(){
        if($_POST){
            $model = new ArtForm();
			$model->member_id = Yii::$app->user->id;
            $model->title = $_POST['title'];
            $model->intro = $_POST['intro'];
            $model->img = $_POST['file-upload'];
            $model->link = $_POST['link'];
			$model->content = $_POST['w0'];
            $model->seokeyword = $_POST['seokeyword'];
            $model->seocontent = $_POST['seocontent'];
            if(!$model->insert()){
				header("Content-type: text/html; charset=utf-8"); 
				}
            return $this->redirect(['art/index']);
        }
	}
	
	//文章列表
	public function actionList(){
		$member_id = Yii::$app->user->id;
		$data = ArtForm::find()->where(['status'=>10,'member_id'=>$member_id])->all();  
        return $this->render('list', [  
            'art' => $data,        
        ]);  			
	}
	
	//在文章列表修改文章
	public function actionUpdate($id){
		$data = ArtForm::find()->where(['id'=>$id])->asArray()->one();	
		return $this->render('update', [  
            'art' => $data,        
        ]); 	  			
	}
	public function actionUpdatedata(){
			$model= ArtForm::findOne(Yii::$app->request->post()['id']);
			$model->title = Yii::$app->request->post()['title'];
			$model->intro = Yii::$app->request->post()['intro'];
			$model->img = Yii::$app->request->post()['file-upload'];
			$model->link = Yii::$app->request->post()['link'];
			$model->content = Yii::$app->request->post()['w0'];
			$model->seokeyword = Yii::$app->request->post()['seokeyword'];
			$model->seocontent = Yii::$app->request->post()['seocontent'];
			if($model->save(false)){
				return $this->redirect(['art/list']);
			}else{
				return $this->goback();
			}	
	}
	//放入回收站
 	public function actionBin($id){
		$model= ArtForm::findOne($id);	
			$model->status = 5;
			if($model->save(false)){
				return $this->redirect(['art/list']);
			}else{
				return $this->goback();
			}
	}
	//恢复文件
	public function actionSbin($id){
		$model= ArtForm::findOne($id);	
			$model->status = 10;
			if($model->save(false)){
				return $this->redirect(['art/bins']);
			}else{
				return $this->goback();
			}
	}
	//回收站
	public function actionBins(){
		$member_id = Yii::$app->user->id;
		$data = ArtForm::find()->where(['status'=>5, 'member_id'=>$member_id])->all();  
        return $this->render('bins', [  
            'art' => $data,        
        ]);  			
	}
	//在回收站修改文章
	public function actionBinupdate($id){
		$data = ArtForm::find()->where(['id'=>$id])->asArray()->one();	
		return $this->render('binupdate', [  
            'art' => $data,        
        ]); 	  			
	}
	public function actionUpdatebin(){
			$model= ArtForm::findOne(Yii::$app->request->post()['id']);
			$model->title = Yii::$app->request->post()['title'];
			$model->intro = Yii::$app->request->post()['intro'];
			$model->img = Yii::$app->request->post()['file-upload'];
			$model->link = Yii::$app->request->post()['link'];
			$model->content = Yii::$app->request->post()['w0'];
			$model->seokeyword = Yii::$app->request->post()['seokeyword'];
			$model->seocontent = Yii::$app->request->post()['seocontent'];
			if($model->save(false)){
				
				return $this->redirect(['art/bins']);
			}else{
				return $this->goback();
			}	
	}
	//在回收站中删除文件
	public function actionBindelete($id){  
		   $model = ArtForm::findOne($id)->delete(); 
           if($model){
				return $this->redirect(['art/bins']);
			}else{
				return $this->goback();
			}	
	}
    	

   

 

}